package com.wwd.movie.login.handel;

import cn.hutool.json.JSONUtil;
import com.wwd.movie.common.AjaxResult;
import com.wwd.movie.common.UserContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import java.io.IOException;

/**
 * 自定义退出处理类 返回成功
 *
 * @author ruoyi
 */
@Configuration
public class LogoutSuccessHandlerImpl implements LogoutSuccessHandler {
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        // 清除用户上下文
        UserContext.clear();

        // 创建退出成功的响应
        AjaxResult result = AjaxResult.success("退出成功");

        // 设置响应头，指定返回类型为 JSON
        response.setContentType("application/json;charset=UTF-8");
        response.setStatus(HttpServletResponse.SC_OK);

        // 返回 JSON 格式的响应内容
        response.getWriter().write(JSONUtil.toJsonStr(result));
        response.getWriter().flush();
    }

}
